package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import au.edu.unsw.editingtool.global.EditingToolException;
import au.edu.unsw.editingtool.global.GlobalConstant;

public class DBLogin extends DBManager {
	/* 1. Setup mapping (the hash values are the db table column name
	 * 2. 
	*/ 
	protected void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("Username", "username");
		MappingTable.put("UID", "uid");
		MappingTable.put("GID", "gid");
		MappingTable.put("Status", "status");
		MappingTable.put("Password", "password");					
		MappingTable.put("Note", "note");
	}
	
	public DBLogin() {
		this.table = "login";
		// Set primary
		this.primaryKeys = new String[]{"Username"};
		// Setup the mapping
		this.setupDBMapping();
	}
	
	public void addLogin(String username, String uid, String gid, String status, String password, String note)throws EditingToolException, ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		Hashtable<String,String> loginInfo = new Hashtable<String,String>();
		loginInfo.put("Username",username);
		loginInfo.put("UID",uid);
		loginInfo.put("GID",gid);
		loginInfo.put("Status",status);
		loginInfo.put("Password",password);
		loginInfo.put("Note",note);
		this.setColumnValues(loginInfo);		
		this.initDB();
		
		this.insert(false, true);
		this.clean();
	}
	
	// check if the login username exists.
	public boolean loginExists(String username) throws ClassNotFoundException, SQLException, EditingToolException {
		boolean result = false;
		int count = 0;
		String fields[] = {"Username"};
		this.setTable(this.table, "");
		this.setFieldsCondition("Username", this.setEqual(username));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		if (this.ResultSet == null) {
			System.out.println("[DBLOGIN] loginExists NULL Resultset");
			this.clean();
			return result;
		}
		while(this.ResultSet.next())
		{
			count++;
		}
		if (count==1)
		{	
			System.out.println("[DBLOGIN] loginExists Resultset Size is 1");
			result = true;			
			this.clean();
			return result;				
		}
		this.clean();		
		return result;			
	}
	
	public boolean isGroup(String username, int group) throws ClassNotFoundException, SQLException, EditingToolException {
		boolean result = false;
		int count = 0;
		String fields[] = {"GID"};
		this.setTable(this.table, "");
		this.setFieldsCondition("Username", this.setEqual(username));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		if (this.ResultSet == null) {
			System.out.println("[DBLOGIN] isGroup NULL Resultset");
			this.clean();
			return result;
		}
		Integer dbgroup = 0;
		while(this.ResultSet.next())
		{
			dbgroup = (Integer) ResultSet.getObject(1);
			count++;
		}
		if (count==1)
		{	
			System.out.println("[DBLOGIN] isGroup Resultset Size = 1");
			if(dbgroup.intValue()==group)
				result = true;						
		}
		this.clean();
		return result;			
	}
	
	public boolean authenticate(String username, String password) throws ClassNotFoundException, SQLException, EditingToolException 
	{		
		boolean result = false;
		if (!this.loginExists(username))
		{
			return result;
		}
		String fields[] = {"Password"};
		this.setTable(this.table, "");
		this.setFieldsCondition("Username", this.setEqual(username));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		if (this.ResultSet == null) {
			System.out.println("[DBLOGIN] authenticate Resultset = NULL");
			this.clean();
			return result;
		}
		
		int count = 0;
		String dbpass = null;
		while(ResultSet.next())
		{
			count ++;
			dbpass = ResultSet.getObject(1).toString();
		}
		
		if (count==1)
		{					
			System.out.println("[DBLOGIN] authenticcate Resultset Size = 1");
			//DEBUG System.out.println("[DBLOGIN] authenticate dbpass = ("+dbpass+") and password = ("+password+")");									
			if(dbpass.compareTo(password)==0)			
				result = true;			
		}
		this.clean();
		return result;						
	}
	
	public String getGroup(String username)throws ClassNotFoundException, SQLException, EditingToolException
	{
		String group=null;
		String fields[] = {"GID"};
		this.setTable(this.table, "");
		this.setFieldsCondition("Username", this.setEqual(username));
		this.addField(fields);	
		
		this.initDB();
		this.selectRecords();
		if (this.ResultSet == null) {
			System.out.println("[DBLOGIN] getGroup Resultset = NULL");
			this.clean();
			return group;
		}
				
		int count = 0;
		while(ResultSet.next())
		{
			count ++;
			group = ResultSet.getObject(1).toString();
		}
		
		if (count==1)
		{
			System.out.println("[DBLOGIN] getGroup Resultset Size = 1");
			this.clean();
			return group;
		}
		else
		{
			this.clean();
			return null;	
		}
	}
}
